﻿Public Class DebitoDAO
    Public Shared Function insertarDebito(ByVal DebitoImporte As Double, ByVal DebitoMotivo As String, ByVal PresentacionId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_debito"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@DebitoId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@DebitoImporte", DebitoImporte).DbType = DbType.Double
                    .AddWithValue("@DebitoMotivo", DebitoMotivo).DbType = DbType.String
                    .AddWithValue("@PresentacionId", PresentacionId).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerNotasDebito(ByVal DebitoId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_debito_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@DebitoId", DebitoId).DbType = DbType.Int32
            da.Fill(ds, "Debitos")

            Return ds.Tables("Debitos")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerDebitosXFiltro(ByVal ObraSocialXPlanId As Integer, ByVal Mes As Integer, ByVal Ano As Integer, ByVal FarmaciaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_Debito_obtenerXFiltro", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@ObraSocialXPlanId", ObraSocialXPlanId).DbType = DbType.Int32
            da.SelectCommand.Parameters.AddWithValue("@Mes", Mes).DbType = DbType.Int32
            da.SelectCommand.Parameters.AddWithValue("@Ano", Ano).DbType = DbType.Int32
            da.SelectCommand.Parameters.AddWithValue("@FarmaciaId", FarmaciaId).DbType = DbType.Int32
            da.Fill(ds, "Debitos")

            Return ds.Tables("Debitos")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarDebito(ByVal DebitoId As Integer, ByVal DebitoImporte As Double, ByVal DebitoMotivo As String, ByVal PresentacionId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_debito"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@DebitoId", DebitoId).DbType = DbType.Int32
                    .AddWithValue("@DebitoImporte", DebitoImporte).DbType = DbType.Int32
                    .AddWithValue("@DebitoMotivo", DebitoMotivo).DbType = DbType.String
                    .AddWithValue("@PresentacionId", PresentacionId).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()

            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarDebito(ByVal DebitoId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_debito"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@DebitoId", DebitoId).DbType = DbType.Int32
                    .AddWithValue("@DebitoImporte", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@DebitoMotivo", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@PresentacionId", Constante.MENOSUNO).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With

            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function
End Class
